<template>
  <div class="search-list-container searchHeight">
    <div class="search-wrapper">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        label-width="auto"
      >
        <el-form-item label="请选择IP段地址" prop="ipAddress">
          <el-select
            v-model="queryParams.ipAddress"
            filterable
            placeholder="请选择"
          >
            <el-option
              v-for="item in ipSelectList"
              :key="item.id"
              :label="item.ipAddress"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="请输入IP段地址" prop="ipHandAddress">
          <el-input
            v-model="queryParams.ipHandAddress"
            placeholder="请输入IP段地址"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          class="search-icon"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form>
    </div>
    <div class="list-wrapper">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="success"
            icon="el-icon-edit"
            size="mini"
            @click="batchImport"
            v-hasPermi="['system:registration:edit']"
            >导入</el-button
          >
        </el-col>
      </el-row>
      <el-table
        ref="tables"
        width="100%"
        height="100%"
        v-loading="loading"
        :data="ipAddressList"
      >
        <el-table-column label="IP地址" align="center" prop="ipAddress">
        </el-table-column>
        <el-table-column
          label="子网掩码"
          align="center"
          prop="subnetMask"
          width="250"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="业务类型"
          align="center"
          prop="businessTypeToString"
        >
        </el-table-column>
        <el-table-column
          label="办公区域"
          align="center"
          prop="addressToString"
        />
        <el-table-column
          label="单位"
          align="center"
          prop="unit"
          width="100"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="使用人"
          align="center"
          prop="ipUser"
          width="100"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="使用人电话"
          align="center"
          prop="phone"
          width="100"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="网关"
          align="center"
          prop="gateway"
          width="100"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="DNS"
          align="center"
          prop="dns"
          width="100"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="MAC地址"
          align="center"
          prop="macAddress"
          width="100"
          :show-overflow-tooltip="true"
        />
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="handleSizeChange"
      />
    </div>
    <!-- 导入对话框 -->
    <el-dialog
      :title="upload.title"
      :visible.sync="upload.open"
      width="400px"
      append-to-body
    >
      <el-upload
        ref="upload"
        :limit="3"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
      >
        <div>1.若无批量导入模板，请先点击【导入模板】进行下载。</div>
        <el-button
          size="small"
          type="primary"
          @click.stop.prevent="importTemplate()"
          >导入模板</el-button
        >
        <div>2.点击【选择文件】,选择按模板填写完成的文件进行导入。</div>
        <el-button size="small" type="primary">选择文件</el-button>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getSelectIPList,
  getIPManagementList,
} from "@/api/businessSupport/businessManagement.js";
import { getToken } from "@/utils/auth";
export default {
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      ipAddressList: [],
      loading: false,
      ipSelectList: [],
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "导入",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/ipInfo/import",
      },
    };
  },
  methods: {
    async getSelectIPList() {
      const result = await getSelectIPList();
      this.ipSelectList = result.data;
    },
    async getIPAddressList() {
      this.loading = true;
      const result = await getIPManagementList(this.queryParams);
      this.loading = false;
      this.ipAddressList = result.data.records;
      this.total = result.data.total;
    },
    handleQuery() {
      this.getIPAddressList();
    },
    resetQuery() {
      this.$refs["queryForm"].resetFields();
      this.queryParams.pageSize = 10;
      this.queryParams.pageNum = 1;
      this.getIPAddressList();
    },
    // 分页控制
    handleSizeChange(newsize) {
      this.queryParams.pageSize = newsize.limit;
      this.getIPAddressList();
    },
    // 批量导入
    batchImport() {
      this.upload.open = true;
    },
    // 下载模板操作
    importTemplate() {
      this.download("/portal/ipInfo/importTemplate", {}, `导入模板.xlsx`);
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(
        "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
          response.msg +
          "</div>",
        "导入结果",
        { dangerouslyUseHTMLString: true }
      );
      this.getIPAddressList();
      if (response.code == 5000) {
        this.download("/portal/ipInfo/exportError", {}, `导入失败的数据.xlsx`);
      }
    },
  },
  mounted() {
    this.getSelectIPList();
    this.getIPAddressList();
  },
};
</script>

<style lang="scss" scoped>
.searchHeight {
  height: calc(100% - 54px) !important;
}
.search-icon {
  margin-left: 30px !important;
}
::v-deep .el-upload {
  text-align: left;
}
::v-deep .el-upload div {
  margin: 10px 0;
}
</style>